<template>
  <div class="reply">
    <!-- 拿到评论 -->
    <van-cell>
      <template #title>
        <div class="artInfo">
          <!-- 头像 -->
          <van-image
            fit="cover"
            round
            width="1.5rem"
            height="1.5rem"
            :src="currentItem.aut_photo"
          />
          <!-- 评论者信息 -->
          <div class="info">
            <div class="name">{{ currentItem.aut_name }}</div>
            <div class="content">{{ currentItem.content }}</div>
            <div class="time">{{ currentItem.pubdate | relvTime }}</div>
          </div>
        </div>
      </template>
    </van-cell>
    <!-- 分割线 -->
    <van-divider>全部回复</van-divider>
    <!-- 回复回复 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <template #title>
          <div class="artInfo">
            <!-- 头像 -->
            <van-image
              fit="cover"
              round
              width="1.5rem"
              height="1.5rem"
              :src="item.aut_photo"
            />
            <!-- 评论者信息 -->
            <div class="info">
              <div class="name">{{ item.aut_name }}</div>
              <div class="content">{{ item.content }}</div>
              <div class="time">
                {{ item.pubdate }}
                >
              </div>
            </div>
          </div>
        </template>
      </van-cell>
    </van-list>

    <!-- 输入内容 -->
    <div class="input-warp">
      <van-field
        v-model="cmtValue"
        @keyup.enter="send"
        center
        clearable
        placeholder="请输入评论内容"
      >
        <template #button>
          <van-button @click="send" size="small" type="info">发布</van-button>
        </template>
      </van-field>
    </div>
  </div>
</template>

<script>
import { cmtListAPI, sendCmtAPI } from '@/api'
export default {
  name: 'reply',
  props: {
    currentItem: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      offset: undefined,
      cmtValue: ''
    }
  },
  methods: {
    async onLoad () {
      const res = await cmtListAPI({
        type: 'c',
        source: this.currentItem.com_id,
        offset: this.offset
      })
      // console.log(res)
      this.list.push(...res.data.data.results)
      // 把本次的最后一条id作为下次请求的参数
      this.offset = res.data.data.last_id
      this.loading = false

      //  当本次的最后一条id，等于 所有数据的结束id就意味着加载完了
      if (res.data.data.end_id === res.data.data.last_id) {
        this.finished = true
      }
    },

    // 发送评论
    async send () {
      // 非空判断
      if (this.cmtValue === '') {
        this.$toast('请输入评论内容')
        return
      }

      // 不为空，拿到数据发请求
      const res = await sendCmtAPI({
        // 文章id
        target: this.currentItem.com_id,
        content: this.cmtValue,
        art_id: this.$route.query.id
      })
      // console.log(res)
      this.list.unshift(res.data.data.new_obj)
      // 回复后回复数加一
      this.currentItem.reply_count++
      // 清空评论框里的数据
      this.cmtValue = ''
      this.$toast('评论成功!')
    }
  }
}
</script>

<style></style>
